package cn.wit.summit.chwan.utils;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.ImageView;

import cn.wit.summit.chwan.widgets.RoundedDrawable;


/**
 * Created by pengsk on 2015/1/17.
 */
public class TipViewUtil {
    public static Drawable getDrawableUseColor(Context context, String bgColor, String borderColor) {
        if (StringUtils.isEmpty(bgColor)) {
            bgColor = "#8dbbec";
        }
        if (StringUtils.isEmpty(borderColor)) {
            borderColor = "#8dbbec";
        }
        ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor(bgColor));
        Drawable      drawable      = RoundedDrawable.fromDrawable(colorDrawable);

        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        if (drawable != null) {
            if (drawable instanceof RoundedDrawable) {
                ((RoundedDrawable) drawable).setBorderColor(Color.parseColor(borderColor));
                ((RoundedDrawable) drawable).setBorderWidth(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1.0f, dm));
                ((RoundedDrawable) drawable).setCornerRadius(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2.0f, dm));
                ((RoundedDrawable) drawable).setScaleType(ImageView.ScaleType.FIT_XY);
            }
        }
        return drawable;
    }

    public static Drawable getDrawableUseColor(Context context, String color) {
//        Drawable d = context.getResources().getDrawable(R.drawable.tip_drawable);
//        if (color.length() == 7 && color.contains("#")) {
//            d.setColorFilter(new LightingColorFilter(Color.parseColor(color), Color.parseColor(color)));
//        } else {
//            d.setColorFilter(new LightingColorFilter(Color.parseColor("#8dbbec"), Color.parseColor("#8dbbec")));
//        }
        if (StringUtils.isEmpty(color)){
            color="#8dbbec";
        }
        ShapeDrawable shapeDrawable = new ShapeDrawable();
//        shapeDrawable.setBounds(0,0,5+30*textNumber,32);
        float destity = SystemInfoUtils.getInstance(context).getDensity(context);
        shapeDrawable.setPadding((int)(5 * destity), 0, (int)(5 * destity), 0);
        shapeDrawable.setBounds(0, 0, 65 * 1, (int) (16f * destity));
//      int corner=  context.getResources().getDimensionPixelOffset(R.dimen.wdp4);
      float corner= 4*SystemInfoUtils.getInstance(context).getDensity();
        float pading=(int)(1*SystemInfoUtils.getInstance(context).getDensity());
        shapeDrawable.setShape(new RoundRectShape(new float[]{corner, corner, corner, corner, corner, corner, corner, corner},
                null, null));
//                new RectF(pading, pading, pading, pading), new float[]{corner, corner, corner, corner, corner, corner, corner, corner}));
        Paint paint = shapeDrawable.getPaint();
        paint.setStyle(Paint.Style.STROKE);
//        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.parseColor(color));
//        paint.setStrokeWidth(context.getResources().getDimensionPixelOffset(R.dimen.wdp1));
        paint.setStrokeWidth((int)(1*SystemInfoUtils.getInstance(context).getDensity()));
        return shapeDrawable;
    }
//    public static Drawable getDrawableUseColorStroke(Context context, String color, int textNumber) {
//        if (StringUtils.isEmpty(color)){
//            color="#8dbbec";
//        }
//        ShapeDrawable shapeDrawable = new ShapeDrawable();
////        shapeDrawable.setBounds(0,0,5+30*textNumber,32);
//        float destity = SystemInfoUtils.getInstance(context).getDensity(context);
//        shapeDrawable.setPadding((int)(5 * destity), 0, (int)(5 * destity), 0);
//        shapeDrawable.setBounds(0, 0, 65 * textNumber, (int) (16f * destity));
//        shapeDrawable.setShape(new RoundRectShape(new float[]{2 * destity, 2 * destity, 2 * destity, 2 * destity, 2 * destity, 2 * destity, 2 * destity, 2 * destity},
//                new RectF(0, 0, 0, 0), new float[]{2 * destity, 2 * destity, 2 * destity, 2 * destity, 2 * destity, 2 * destity, 2 * destity, 2 * destity}));
//        Paint paint = shapeDrawable.getPaint();
//        paint.setStyle(Paint.Style.STROKE);
//        paint.setColor(Color.parseColor(color));
//        paint.setStrokeWidth(1);
//
//
//        return shapeDrawable;
//    }
    public static Drawable getDrawableUseColorStroke(Context context, String color, int textNumber) {
        if (StringUtils.isEmpty(color)){
            color="#8dbbec";
        }
        ShapeDrawable shapeDrawable = new ShapeDrawable();
//        shapeDrawable.setBounds(0,0,5+30*textNumber,32);
        float destity = SystemInfoUtils.getInstance(context).getDensity(context);
        shapeDrawable.setBounds(0, 0, 65 * textNumber, (int) (16f * destity));
        shapeDrawable.setShape(new RoundRectShape(new float[]{4 * destity, 4 * destity, 4 * destity, 4 * destity, 4 * destity, 4 * destity, 4 * destity, 4 * destity},
                new RectF(0, 0, 0, 0), new float[]{4 * destity, 4 * destity, 4 * destity, 4 * destity, 4 * destity, 4 * destity, 4 * destity, 4 * destity}));
        Paint paint = shapeDrawable.getPaint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.parseColor(color));
        paint.setStrokeWidth(2);


        return shapeDrawable;
    }
}
